package com.on.smile.controller;

import com.on.smile.entity.UserEntity;
import com.on.smile.service.UserService;
import com.on.smile.util.MD5Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/api/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/select")
    @ResponseBody
    public String login(String username, String password) {
        if (username == null || password == null || "".equals(password) || "".equals(username)) {
            return "账号密码不能为空";
        } else {
            return userService.login(username, password);
        }
    }

    @PostMapping("/insert")
    @ResponseBody
    public String register(String username, String password) {
        if (username == null || password == null || "".equals(username) || "".equals(password)) {
            return "无法注册";
        } else {
            // 2025-06-21 11:37:00 你俩说懂了
            // 创建对象
            UserEntity user2 = new UserEntity();
            // 赋值
            user2.setUsername(username);
            user2.setPassword(password);

            // 传参
            String registerResult = userService.register(user2);

            return registerResult;
        }
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public String updatePassword(Long id, String newpassword) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(id);
        userEntity.setPassword(newpassword);
        return userService.updatePassword(userEntity);
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public String deleteById(Long id) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(id);
        return userService.deleteById(userEntity);
    }

    @PostMapping("/selectByPage")
    @ResponseBody
    public List<UserEntity> selectByPage(Long page, Long size) {
        if (page == null || size == null || page <= 0 || size <= 0) {
            System.out.println("参数不合法");
        }
            return userService.selectByPage(page, size);

    }
}
